package plugincleaner.preferences;

import java.util.ArrayList;

import org.eclipse.jface.preference.FieldEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class MyTableFieldEditor extends FieldEditor {

	private String gLabelText;
	private boolean gHasCheckbox;
	private PluginTableController gController;

	private Label gLabel;

	private Table gPluginTable;

	protected MyTableFieldEditor() {
		// No default behavior
	}

	public MyTableFieldEditor(String name, String labelText, Composite parent,
			boolean hasCheckbox, PluginTableController controller) {
		super(name, labelText, parent);

		this.gLabelText = labelText;
		this.gHasCheckbox = hasCheckbox;
		this.gController = controller;

		if (this.gLabelText.length() > 0) {
			this.gLabel = new Label(parent, SWT.BOTTOM | SWT.LEFT_TO_RIGHT);
			this.gLabel.setText(this.gLabelText);
			this.gLabel.setSize(100, 50);
			this.gLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
					false));
			this.gLabel.setVisible(true);
		}

		this.gPluginTable = new Table(parent, SWT.MULTI | SWT.BORDER
				| SWT.FULL_SELECTION | (this.gHasCheckbox ? SWT.CHECK : 0));

		this.gPluginTable.setLinesVisible(true);
		this.gPluginTable.setHeaderVisible(true);

		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
		data.heightHint = 150;
		this.gPluginTable.setLayoutData(data);

		String[] titles = { "Plugin" };
		for (int i = 0; i < titles.length; i++) {
			TableColumn column = new TableColumn(this.gPluginTable, SWT.NONE);
			column.setText(titles[i]);
			column.pack();
		}
		
		this.gPluginTable.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				gController.updateOldPluginTable(((TableItem)event.item).getText());
			}			
		});
	}
	
	public void addItem(String plugin) {
		TableItem item = new TableItem(this.gPluginTable, SWT.NONE);
		item.setChecked(false);
		item.setText(0, plugin);
		
		this.gPluginTable.getColumn(0).pack();
	}
	
	public void removeAll() {
		this.gPluginTable.removeAll();
	}
	
	public ArrayList<String> getSelectedItems() {
		if(this.gHasCheckbox){
			ArrayList<String> toReturn = new ArrayList<String>();
			for(TableItem item : this.gPluginTable.getItems()){
				if(item.getChecked()){
					toReturn.add(item.getText(0));
				}
			}
			
			return toReturn;
		}
		
		return null;
	}

	@Override
	protected void adjustForNumColumns(int numColumns) {
		((GridData) this.gPluginTable.getLayoutData()).horizontalSpan = numColumns;
	}

	@Override
	protected void doFillIntoGrid(Composite parent, int numColumns) {
	}

	@Override
	protected void doLoad() {
	}

	@Override
	protected void doLoadDefault() {
	}

	@Override
	protected void doStore() {
	}

	@Override
	public int getNumberOfControls() {
		return 2;
	}

}
